package com.supply.dl.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import butterknife.BindView;

import com.supply.dl.R;
import com.supply.dl.app.http.BaseInfo;
import com.supply.dl.bean.LoginResponse;
import com.supply.dl.global.Global;
import com.supply.dl.http.HttpManger;
import com.supply.dl.utils.StorageUtil;
import com.supply.dl.utils.utils.ToastUtil;

/**
 * 登陆界面
 *
 * @author antao
 * @since 2018-4-27
 */
public class SupplyLoginActivity extends BaseActivity {
    @BindView(R.id.txt_setIp)
    TextView txtSetIp;

    @BindView(R.id.txt_setDefaultIp)
    TextView txtSetDefaultIp;

    @BindView(R.id.txt_login)
    TextView txtLogin;

    @BindView(R.id.edit_username)
    EditText editTextName;
    @BindView(R.id.edit_password)
    EditText editPwd;

    private LoginResponse loginResult;

    @Override
    protected int getLayoutResource() {
        return R.layout.supply_login_layout;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setToolBarInVisable();
        txtLogin.setOnClickListener(this);
        txtSetIp.setOnClickListener(this);
        txtSetDefaultIp.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.txt_login:
                startActivity(new Intent(this, MainActivity.class));
                tryLogin();
                break;
            case R.id.txt_setIp:
                startActivity(new Intent(this, SetIpAndPortActivity.class));
                break;
            case R.id.txt_setDefaultIp:
                Global.getInstance().setHTTPAddress("");
                ToastUtil.showToast(this, "设置的网络地址清除成功！");
                break;
        }
    }

    private void tryLogin() {
        String username = editTextName.getText().toString().trim();
        String pwd = editPwd.getText().toString().trim();
        //固定测试账户
        username = "15928658740";
        pwd = "123456";
        HttpManger httpManger = HttpManger.getInstance();
        httpManger.setContext(this);
        httpManger.tryLogin(username, pwd);
        httpManger.setHttpResponseCallBackListener(new HttpManger.HttpResponseCallBackListener() {
            @Override
            public void onSuccess(BaseInfo<?> baseInfo) {
                loginResult = (LoginResponse) baseInfo.getData();
                Global.getInstance().setLoginResult(loginResult);
                StorageUtil.saveUserToken(SupplyLoginActivity.this, loginResult.userId);
            }

            @Override
            public void onFailure(String message) {
                ToastUtil.showToast(SupplyLoginActivity.this, message);
                /**
                 * 注意：以下为测试代码
                 * 为了测试方便，后台已写死userId值
                 */
                loginResult=new LoginResponse();
                loginResult.setUserId("2c90881e60596fd90160678463dc008f");
                loginResult.setStatus(0);
                Global.getInstance().setLoginResult(loginResult);
            }
        });

    }
}
